use anyhow::Result;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::{Form, Json};
use log::info;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use utoipa::ToSchema;

use crate::handler::common::AppResult;

#[derive(Debug, Deserialize, ToSchema)]
pub struct LoginForm {
    #[schema(example = "admin")]
    username: String,
    #[schema(example = "password")]
    password: String,
}

#[derive(Serialize, ToSchema)]
pub struct LoginResponse {
    #[schema(example = "ok")]
    status: String,
    #[schema(example = "account")]
    r#type: String,
    #[schema(example = "admin")]
    #[serde(rename = "currentAuthority")]
    current_authority: String,
}

#[derive(Serialize, ToSchema)]
pub struct CommonResponse<T> {
    data: Option<T>,
    #[schema(example = true)]
    success: bool,
}

/// 用户登录
#[utoipa::path(
    post,
    path = "/api/login",
    tag = "auth",
    summary = "用户登录",
    description = "使用用户名和密码进行登录",
    request_body = LoginForm,
    responses(
        (status = 200, description = "登录成功", body = LoginResponse),
        (status = 400, description = "登录失败")
    )
)]
pub async fn login(Json(login): Json<LoginForm>) -> Response {
    info!("login form: {:?}", login);

    (StatusCode::OK, AppResult::<()>::success()).into_response()
}

/// 用户登出
#[utoipa::path(
    post,
    path = "/api/login/outLogin",
    tag = "auth",
    summary = "用户登出",
    description = "退出当前登录状态",
    responses(
        (status = 200, description = "登出成功", body = CommonResponse<Value>)
    )
)]
pub async fn logout() -> Result<Json<Value>, StatusCode> {
    Ok(Json(json!({ "data": {}, "success": true })))
}

/// 获取当前用户信息
#[utoipa::path(
    get,
    path = "/api/currentUser",
    tag = "user",
    summary = "获取当前用户信息",
    description = "获取当前登录用户的详细信息",
    responses(
        (status = 200, description = "获取成功", body = CommonResponse<Value>)
    )
)]
pub async fn current_user() -> Result<Json<Value>, StatusCode> {
    Ok(Json(json!({
        "success": true,
        "data": {
            "name": "Serati Ma",
            "avatar": "https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png",
            "userid": "00000001",
            "email": "antdesign@alipay.com",
            "signature": "海纳百川，有容乃大",
            "title": "交互专家",
            "group": "蚂蚁金服－某某某事业群－某某平台部－某某技术部－UED",
            "tags": [
                {
                    "key": "0",
                    "label": "很有想法的"
                },
                {
                    "key": "1",
                    "label": "专注设计"
                },
                {
                    "key": "2",
                    "label": "辣~"
                },
                {
                    "key": "3",
                    "label": "大长腿"
                },
                {
                    "key": "4",
                    "label": "川妹子"
                },
                {
                    "key": "5",
                    "label": "海纳百川"
                }
            ],
            "notifyCount": 12,
            "unreadCount": 11,
            "country": "China",
            "access": "admin",
            "geographic": {
                "province": {
                    "label": "浙江省",
                    "key": "330000"
                },
                "city": {
                    "label": "杭州市",
                    "key": "330100"
                }
            },
            "address": "西湖区工专路 77 号",
            "phone": "0752-268888888"
        }
    })))
}
